package pers.cnzdy.tutorial.Chapter5_UI.Section1_UIDesign;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import pers.cnzdy.tutorial.Chapter5_UI.Section3_Event.AnswerActivity;
import pers.cnzdy.tutorial.Quiz;
import pers.cnzdy.tutorial.R;

/**
 * 演示课堂练习
 * @author cnzdy
 */
public class QuizActivity extends AppCompatActivity {
    static final String TAG = "QuizActivity";
    static final String CURRENT_INDEX = "quiz_index";
    static final int REQUEST_ANSWER_CODE = 0;

    Button trueBtn;
    Button falseBtn;
    Button nextBtn;
    Button checkAnswerBtn;
    TextView quizTextView;

    // TODO 以后添加新的题目
    Quiz[] quizSet = new Quiz[] {
            new Quiz(R.string.quiz_activity, true),
            new Quiz(R.string.quiz_service, false),
            new Quiz(R.string.quiz_layout, false),
            new Quiz(R.string.quiz_intent, true),
            new Quiz(R.string.quiz_handler, true),
    };

    int currentIndex = 0;
    boolean isShown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);

        quizTextView = findViewById(R.id.text_view_quiz);
        trueBtn = findViewById(R.id.true_button);
        falseBtn = findViewById(R.id.false_button);
        checkAnswerBtn = findViewById(R.id.check_answer_button);

        trueBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(true);
            }
        });

        falseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(false);
            }
        });

        checkAnswerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean answer = false; // quizSet[currentIndex].isAnswer();
                int  statementId = quizSet[currentIndex].getStatementId();

//                Intent i = AnswerActivity.newIntent(QuizActivity.this,
//                        statementId, answer);
//                startActivity(i);

                //-----------------------------------//
                // 动态打开Activity
                Intent i = new Intent("pers.cnzdy.tutorial.ACTION_START");
                startActivity(i);

                //----------------------------------//
                //startActivityForResult(i, REQUEST_ANSWER_CODE);
            }
        });

        if (savedInstanceState != null) {
            currentIndex = savedInstanceState.getInt(CURRENT_INDEX, 0);
        }

        updateQuiz();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent i) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }

        if (requestCode == REQUEST_ANSWER_CODE) {
            if (i == null) {
                return;
            }
            isShown = AnswerActivity.wasAnswerShown(i);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Log.i(TAG, "onSaveInstanceState");
        savedInstanceState.putInt(CURRENT_INDEX, currentIndex);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.quiz_menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_add_quiz:
                Toast.makeText(this, R.string.add_quiz, Toast.LENGTH_SHORT).show();
                break;
            case R.id.item_remove_quiz:
                Toast.makeText(this, R.string.remove_quiz, Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }

    private void updateQuiz() {
//        Log.d(TAG, "下一个问题，问题 #" + currentIndex, new Exception());
        int quiz = quizSet[currentIndex].getStatementId();
        quizTextView.setText(quiz);
        isShown = false;
    }

    /**
     * @param userChoice
     */
    private void checkAnswer(boolean userChoice) {
        boolean answer = false;//quizSet[currentIndex].isAnswer();

        toast("String msg");

        int messageId = 0;

        if (isShown) {
            messageId = R.string.check_answer_toast;
        } else {
            if (userChoice == answer) {
                messageId = R.string.correct_toast;
            } else {
                messageId = R.string.incorrect_toast;
            }
        }
        Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
    }

    void toast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    public void nextQuiz(View view) {
        currentIndex = (currentIndex + 1) % quizSet.length;
        updateQuiz();
    }
}
